﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using HandyControl.Controls;
using HandyControl.Data;
using YJTTool.Common.Controls;
using YJTTool.Common.Enums;
using YJTTool.Common.Events;
using YJTTool.Common.Managers;
using YJTTool.Core.Utilities;
using YJTTool.DBBaseServer.AbstractModel;

namespace YJTTool.Common.Helper
{
    public class TimerHelper
    {
        private Dictionary<TimerEventEnum,TimerTaskManager> timerTask;
        private static readonly Lazy<TimerHelper> _instance = new Lazy<TimerHelper>(() => new TimerHelper());
        public static TimerHelper Instance => _instance.Value;
        private static readonly Lazy<AbstractTimerTaskServer>  abstractTimerTask = new Lazy<AbstractTimerTaskServer>();
        public static AbstractTimerTaskServer AbstractTimerTask => abstractTimerTask.Value;
        private TimerHelper()
        {
            TimerEvent.Instance.Subscribe(SetTimer);
            timerTask = new Dictionary<TimerEventEnum, TimerTaskManager>();
        }
        public void Init() 
        {
            TimerTaskInit();
            
        }
        private void SetTimer(TimerEventEnum @enum)
        {
            switch (@enum)
            { 
                case TimerEventEnum.ScheduledReminders:
                    TimerTaskInit();
                    break;
            }
        }
        private  void TimerTaskInit() 
        {
            if (!timerTask.ContainsKey(TimerEventEnum.ScheduledReminders))
            {
                timerTask[TimerEventEnum.ScheduledReminders] = new TimerTaskManager();
            }
            else
            {
                timerTask[TimerEventEnum.ScheduledReminders] = new TimerTaskManager();
            }
            var data =  AbstractTimerTask.Query();
            TimerTask timer = new TimerTask();
            var Timers = new ObservableCollection<TimerTask>();
            foreach (var item in data)
            {
                Timers.Add(timer.DbtoDto(item));
            }
            List<ScheduledTask> _scheduledtimers = new List<ScheduledTask>();
            foreach (var item in Timers)
            {
                if (item.IsAvailability == true)
                {
                    _scheduledtimers.Add(new ScheduledTask(
                                             taskTime: new TimeSpan(int.Parse(item.Hour), int.Parse(item.Minute), int.Parse(item.Second)),  // 设定时分秒
                                             repeatCount: int.Parse(item.Repeat),                    // 重复执行2次
                                             interval: TimeSpan.FromMinutes(int.Parse(item.Interval)), // 每10分钟执行一次
                                             taskAction: () => { Application.Current.Dispatcher.Invoke(() => { Notification.Show(new AppNotification(item.TimerName, item.Message), ShowAnimation.Fade, true); }); } // 执行的任务方法
                                             ));
                }
            }
            foreach (var item in _scheduledtimers)
            {
                timerTask[TimerEventEnum.ScheduledReminders].AddTask(item);
            }
            timerTask[TimerEventEnum.ScheduledReminders].StartTasks();
        }
    }
}
